﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Text;

namespace SP_XHWeb.ControlTemplates.SP_XHWeb
{
    public partial class PersonalOffice : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lbloffice.InnerHtml = InitTopOffice();
            }
        }

        private string InitTopOffice()
        {
            StringBuilder str = new StringBuilder();
            List<SPListItem> lists = GetLinks();

            str.Append("<ul>");

            if (lists != null && lists.Count != 0)
            {
                if (lists.Count > 10)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        str.Append("<li>");
                        str.AppendFormat(lists[i]["Picture"].ToString());
                        str.AppendFormat("<a href='{0}'>{1}</a>", lists[i]["LinkUrl"], lists[i].Title);
                        str.Append("</li>");
                    }
                }

                else
                {
                    for (int i = 0; i < lists.Count; i++)
                    {
                        str.Append("<li>");
                        str.AppendFormat(lists[i]["Picture"].ToString());
                        str.AppendFormat("<a href='{0}'>{1}</a>", lists[i]["LinkUrl"], lists[i].Title);
                        str.Append("</li>");
                    }
                }
            }
            str.Append("</ul>");
            return str.ToString();
        }


        //项目信息需要显示的信息
        private List<SPListItem> GetLinks()
        {
            List<SPListItem> lists = new List<SPListItem>();
            //  http://
            string listUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            using (SPSite site = new SPSite(listUrl + "/default.aspx"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("PersonalOffice");
                    if (list != null && list.ItemCount != 0)
                    {
                        foreach (SPListItem item in list.Items)
                        {
                            if (item.Fields.ContainsField("LinkUrl"))
                            {
                                if (item["LinkUrl"] != null && !string.IsNullOrEmpty(item["LinkUrl"].ToString()))
                                {
                                    lists.Add(item);
                                }
                            }
                        }
                    }
                }
            }
            return lists;
        }
    }
}
